import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    OnInit,
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';
import {
    ComboboxItemDto,
    OrganizationUnitServiceProxy,
    NameValueDto,
    UserWorkingTimeServiceProxy,
    GetUserWorkingTimeDto,
} from '@shared/service-proxies/service-proxies';
import { sum } from 'lodash';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';

@Component({
    templateUrl: './workingtimeform.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class WorkingtimeformComponent extends AppComponentBase
    implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';
    orgid: ComboboxItemDto;
    teamGroups: ComboboxItemDto[] = [];
    public startbDate: moment.Moment = moment().startOf("day");
    public endbDate: moment.Moment = moment().endOf("day");
    public startuDate: moment.Moment = moment().startOf("day");
    public enduDate: moment.Moment = moment().endOf("day");
    users = new Array<NameValueDto>();
    userName = new NameValueDto();
    sumworking = 0;
    userWorkingTime = new GetUserWorkingTimeDto();
    tab = 0;
    userWorkCount = 0;
    constructor(
        injector: Injector,
        private _fileDownloadService: FileDownloadService,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy,
        private _userWorkingTimeServiceProxy: UserWorkingTimeServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._organizationUnitServiceProxy
            .getTeamGroups()
            .subscribe((result) => {
                this.teamGroups = result.items;
                this.orgid = this.teamGroups[0];
                this.getBWorking();
            });
        this._organizationUnitServiceProxy
            .findUsersForNode()
            .subscribe((result) => {
                this.users = result;
                this.userName = this.users[0];
            });
    }
    getBWorking(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();
        // let start = null;
        // let end = null;
        // this.sumworking = 0;
        // if (this.dateBRange !== undefined) {
        //     start = moment(this.dateBRange[0]);
        //     end = moment(this.dateBRange[1]);
        // }

        this._userWorkingTimeServiceProxy
            .getWorkingTimeForm(
                this.startbDate,
                this.endbDate,
                this.orgid.displayText,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe((result) => {
                console.log(result.items);
                result.items.forEach((item) => {
                    this.sumworking = this.sumworking + item.workingTime;
                });
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    cleanB() {
        this.orgid = this.teamGroups[0];
        //this.dateBRange = undefined;
        this.getBWorking();
    }
    getUWorking() {
        this.primengTableHelper.showLoadingIndicator();
        // let start = null;
        // let end = null;
        // if (this.dateURange !== undefined) {
        //     start = moment(this.dateURange[0]);
        //     end = moment(this.dateURange[1]);
        // }
        this._userWorkingTimeServiceProxy
            .getUserWorkingTime(
                this.startuDate,
                this.enduDate,
                this.userName.value,
                this.filterText
            )
            .subscribe((result) => {
                this.userWorkingTime = result;
                this.userWorkCount = result.getUserWorkingTimeDetail==undefined?0:result.getUserWorkingTimeDetail.length;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    cleanU() {
       // this.dateURange = undefined;
        this.filterText = undefined;
        this.getUWorking();
    }
    onTabChange(event) {

        if (event.index == 0) {
            this.tab = 0;
            this.getBWorking();
        } else if (event.index == 1) {
            this.tab = 1;
            this.getUWorking();
        }
    }
    exportToExcel(): void {
        // let start = null;
        // let end = null;


        if (this.tab == 0) {
            // if (this.dateBRange !== undefined) {
            //     start = moment(this.dateBRange[0]);
            //     end = moment(this.dateBRange[1]);
            // }
            this._userWorkingTimeServiceProxy
                .getWorkingTimeToExcel(
                    this.startbDate,
                    this.endbDate,
                    this.orgid.displayText,
                    this.filterText,
                    this.primengTableHelper.getSorting(this.dataTable),
                    1,
                    0
                )
                .subscribe((result) => {
                    this._fileDownloadService.downloadTempFile(result);
                });
        } else {
            // if (this.dateURange !== undefined) {
            //     start = moment(this.dateURange[0]);
            //     end = moment(this.dateURange[1]);
            // }
            this._userWorkingTimeServiceProxy
                .getUserWorkingTimeToExcel(
                    this.startuDate,
                this.enduDate,
                    this.userName.value,
                    this.filterText
                )
                .subscribe((result) => {
                    this._fileDownloadService.downloadTempFile(result);
                });
        }
    }
    BanDetailToExcel(): void {
        // let start = null;
        // let end = null;
        // console.log(this.dateBRange);
        // if (this.dateBRange !== undefined) {
        //     start = moment(this.dateBRange[0]);
        //     end = moment(this.dateBRange[1]);
        // }
        this._userWorkingTimeServiceProxy
            .banDetailToExcel(
                this.orgid.displayText,
                this.startbDate,
                this.endbDate
            )
            .subscribe((result) => {
                this._fileDownloadService.downloadTempFile(result);
            });

    }
}
